<?php
namespace AdminBundle\Repository;


class BaseWebchatUserRepository extends BaseRepository
{
    public function pageQuery($_params, $page)
    {
        $sql = <<<EOF
select 
    id, nickname, user_name as userName, mobile, password, note, is_enabled as isEnabled, 
    device_id as deviceId, device_text as deviceText,ctime
from base_webchat_user 
where 1=1 
EOF;
        $params = array();
        if (!empty($_params)) {
/*            if (isset($_params['starttime'])) {
                $sql .= ' and ctime > :starttime ';
                $params['starttime'] = $_params['starttime'];
            }
            if (isset($_params['endtime'])) {
                $sql .= ' and ctime < :endtime ';
                $params['endtime'] = $_params['endtime'];
            }*/
            if (isset($_params['keyword']) && !empty($_params['keyword'])) {
                $sql .= ' and nickname like :keyword ';
                $params['keyword'] = '%' . $_params['keyword'] . '%';
            }
        }
        $sql .= ' ORDER BY id DESC';

        return $this->pageWithRawSql($sql, $params, $page);
    }

    /**
     * 根据设备id,获取已经分配的微信详细信息
     * 用途: 后台定时任务分发任务
     * @param $_params
     * @param int $page
     * @return bool
     * @throws \Exception
     */
    public function wechatByDevice($_params)
    {
        $sql = <<<EOF
SELECT 
    id, user_name AS userName,nickname
FROM
    base_webchat_user
WHERE
    is_enabled = 1 AND device_id = :device_id;
EOF;
        $params = ['device_id' => $_params['device_id']];

        return $this->rawQuery($sql, $params);

    }

    public function lastRoomByUserId($user_id, $room_type = 1, $count = 1)
    {
        $sql = <<<EOF
SELECT 
    room_id AS id,
    room_name AS name,
    room_type AS status,
    room_tags AS note,
    status as progress,
    room_qrcode AS qrcode
FROM
    log_room_user
WHERE
    webchat_id = :webchat_id
    AND room_type = :room_type
ORDER BY ID DESC
LIMIT {$count}
EOF;
        $params = ['webchat_id' => $user_id, 'room_type' => $room_type];

        return $this->rawQuery($sql, $params);
    }
}
